package com.heo.pojo;

import java.math.BigDecimal;

import com.heo.util.ByteHelper;
import com.heo.util.Utils;

public class LocationInfoEntity extends BaseEntity {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int reserve;
	private int status;
	private String latitude;
	private String longitude;
	private int height;
	private int speed;
	private int direction;
	private String time;
	
	private int signal;   //无线通信网络信号强度 
	private int locationMode;   //定位方式  0 是 LBS 定位， 否则就是 GPS 定位。 
	private int openSize; //开锁次数 
	private int lockStatus; //锁状态
	private String uploadType; //0000 表示定时上传 0001 表示开锁上传 0010 表示关锁上传 0011 表示低电上传 0100 表示移动上传 0101 表示撞击上传
	private int batteryStatus; //0 表示放电，1 表示正在充电 
	private int batteryElectricity; //电池电量
	private int batteryVoltage; //电池电压
	private int chargingVoltage;//充电电压
	private int serialStatus;// 0 表示正常， 1 表示串口异常 用于二合一锁 
	private int electronicFence;//电子围栏  ， 车辆是否停放在电子围 栏范围内（只在关锁上报时有效） 0 表示不在围栏内，1 表示在 围栏内
	
	public LocationInfoEntity(String bodyHex) {
		String reserve = bodyHex.substring(0, 8);
		String status = bodyHex.substring(8, 16);
		String latitudeStr = bodyHex.substring(16, 24);
		String longitudeStr = bodyHex.substring(24, 32);
		String heightStr = bodyHex.substring(32, 36);
		String speedStr = bodyHex.substring(36, 40);
		String directionStr = bodyHex.substring(40, 44);
		long latitudeLong = ByteHelper.hex16ToInt(latitudeStr);
		long longitudeLong = ByteHelper.hex16ToInt(longitudeStr);
		if(latitudeLong > 0) {
			latitude = divide(latitudeLong, 1000000d, 6).toString();
		}
		if(longitudeLong > 0) {
			longitude = divide(longitudeLong, 1000000d, 6).toString();
		}
		height = ByteHelper.hex16ToInt(heightStr);
		speed = ByteHelper.hex16ToInt(speedStr);
		direction = ByteHelper.hex16ToInt(directionStr);
		time = bodyHex.substring(44, 56);
		String signalHex = bodyHex.substring(56, 62);
		if(signalHex.startsWith("30")) {
			signal = ByteHelper.hex16ToInt(signalHex.substring(4));
		}
		
		String locationHex = bodyHex.substring(62, 68);
		if(locationHex.startsWith("31")) {
			locationMode = ByteHelper.hex16ToInt(locationHex.substring(4));
		}
		
		int start = 70;
		String e1Hex = bodyHex.substring(68, 70);
		if("E1".equals(e1Hex)) {
			int length = ByteHelper.hex16ToInt(bodyHex.substring(70, 72));
			start = 72 + length * 2;
			openSize = ByteHelper.hex16ToInt(bodyHex.substring(72, start));
		}
		
		String e2Hex = bodyHex.substring(start, start + 2);
		if("E2".equals(e2Hex)) {
			start = start + 2;
			int length = ByteHelper.hex16ToInt(bodyHex.substring(start, start + 2));
			String openStatusStr = bodyHex.substring(start + 2, start + 2 + length * 2);
			String binStr = ByteHelper.hexString2binaryString(openStatusStr).substring(8);
			System.out.println(binStr);
			lockStatus = Utils.getInteger(binStr.substring(7));
			uploadType = binStr.substring(3, 7);
			batteryStatus = Utils.getInteger(binStr.substring(1, 2));
			start = start + 2 + length * 2;
		}
		
		String e3Hex = bodyHex.substring(start, start + 2);
		if("E3".equals(e3Hex)) {
			start = start + 2;
			int length = ByteHelper.hex16ToInt(bodyHex.substring(start, start + 2));
			String batteryStr = bodyHex.substring(start + 2, start + 2 + length * 2);
			String batteryElectricityStr = batteryStr.substring(0, 4);
			String batteryVoltageStr = batteryStr.substring(4, 8);
			String chargingVoltageStr = batteryStr.substring(8, 12);
			
			batteryElectricity = ByteHelper.hex16ToInt(batteryElectricityStr);  //电池电量
			batteryVoltage = ByteHelper.hex16ToInt(batteryVoltageStr);  //电池电压
			chargingVoltage = ByteHelper.hex16ToInt(chargingVoltageStr);
			start = start + 2 + length * 2;
		}
		
		String e4Hex = bodyHex.substring(start, start + 2);
		if("E4".equals(e4Hex)) {
			start = start + 2;
			int length = ByteHelper.hex16ToInt(bodyHex.substring(start, start + 2));
			start = start + 2 + length * 2;
		}
		if(bodyHex.length()<(start + 2)) {
			return;
		}
		String e5OrE6Hex = bodyHex.substring(start, start + 2);
		boolean e6Read = false;
		if("E5".equals(e5OrE6Hex)) {
			start = start + 2;
			int length = ByteHelper.hex16ToInt(bodyHex.substring(start, start + 2));
			start = start + 2 + length * 2;
		}else if("E6".equals(e5OrE6Hex)) {
			e6Read = true;
			start = start + 2;
			int length = ByteHelper.hex16ToInt(bodyHex.substring(start, start + 2));
			String electronicFenceStr = bodyHex.substring(start + 2, start + 2 + length * 2);
			byte[] b2 = ByteHelper.hexStringToBytes(electronicFenceStr);
			electronicFence = Utils.getInteger(ByteHelper.conver2HexStr(b2));
			start = start + 2 + length * 2;
		}
		
		if(!e6Read) {
			if(bodyHex.length()<(start + 2)) {
				return;
			}
			String e6Hex = bodyHex.substring(start, start + 2);
			if("E6".equals(e6Hex)) {
				e6Read = true;
				start = start + 2;
				int length = ByteHelper.hex16ToInt(bodyHex.substring(start, start + 2));
				String electronicFenceStr = bodyHex.substring(start + 2, start + 2 + length * 2);
				byte[] b2 = ByteHelper.hexStringToBytes(electronicFenceStr);
				electronicFence = Utils.getInteger(ByteHelper.conver2HexStr(b2));
				start = start + 2 + length * 2;
			}
		}
		
	}
	
	public int getReserve() {
		return reserve;
	}
	public void setReserve(int reserve) {
		this.reserve = reserve;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public String getLatitude() {
		return latitude;
	}
	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}
	public String getLongitude() {
		return longitude;
	}
	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getSpeed() {
		return speed;
	}
	public void setSpeed(int speed) {
		this.speed = speed;
	}
	public int getDirection() {
		return direction;
	}
	public void setDirection(int direction) {
		this.direction = direction;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public int getSignal() {
		return signal;
	}
	public void setSignal(int signal) {
		this.signal = signal;
	}
	public int getLocationMode() {
		return locationMode;
	}
	public void setLocationMode(int locationMode) {
		this.locationMode = locationMode;
	}
	public int getOpenSize() {
		return openSize;
	}
	public void setOpenSize(int openSize) {
		this.openSize = openSize;
	}
	public int getLockStatus() {
		return lockStatus;
	}
	public void setLockStatus(int lockStatus) {
		this.lockStatus = lockStatus;
	}
	public String getUploadType() {
		return uploadType;
	}
	public void setUploadType(String uploadType) {
		this.uploadType = uploadType;
	}
	public int getBatteryStatus() {
		return batteryStatus;
	}
	public void setBatteryStatus(int batteryStatus) {
		this.batteryStatus = batteryStatus;
	}
	public int getBatteryElectricity() {
		return batteryElectricity;
	}
	public void setBatteryElectricity(int batteryElectricity) {
		this.batteryElectricity = batteryElectricity;
	}
	public int getBatteryVoltage() {
		return batteryVoltage;
	}
	public void setBatteryVoltage(int batteryVoltage) {
		this.batteryVoltage = batteryVoltage;
	}
	public int getChargingVoltage() {
		return chargingVoltage;
	}
	public void setChargingVoltage(int chargingVoltage) {
		this.chargingVoltage = chargingVoltage;
	}
	public int getSerialStatus() {
		return serialStatus;
	}
	public void setSerialStatus(int serialStatus) {
		this.serialStatus = serialStatus;
	}
	public int getElectronicFence() {
		return electronicFence;
	}
	public void setElectronicFence(int electronicFence) {
		this.electronicFence = electronicFence;
	}
	
	public static void main(String[] args) {
		String bodyHex = "000000000000000201594A6806CCA84800DC000100F118040915214630011F31010CE10400000026E2020000E306005801960000E400DC";
		LocationInfoEntity locationInfo = new LocationInfoEntity(bodyHex);
//		System.out.println(divide(114075720d, 1000000d, 6));
	}
	
	 public Double divide(double d1,double d2,int len) {// 进行除法运算
         BigDecimal b1 = new BigDecimal(d1);
         BigDecimal b2 = new BigDecimal(d2);
        return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue();

     }
}
